{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq import Overlay, allocate\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['axi_dma', 'blinkled_0/saxi'])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bitfile = \"thread_stream_axi_stream_fifo.bit\"\n",
    "overlay = Overlay(bitfile)\n",
    "overlay.ip_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "dma = overlay.axi_dma\n",
    "blinkled = overlay.blinkled_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "reduce_size = 8\n",
    "read_size = 1024\n",
    "write_size = read_size // reduce_size\n",
    "\n",
    "src = allocate(shape=(read_size,), dtype=np.int32)\n",
    "dst = allocate(shape=(write_size,), dtype=np.int32)\n",
    "bias = allocate(shape=(write_size,), dtype=np.int32)\n",
    "\n",
    "bias_addr = bias.physical_address"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "source": [
    "src[:] = np.arange(read_size, dtype=np.int32)\n",
    "dst[:] = np.zeros([write_size], dtype=np.int32)\n",
    "bias[:] = np.ones([write_size], dtype=np.int32)\n",
    "print(dst[-16:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "dma.sendchannel.transfer(src)\n",
    "dma.recvchannel.transfer(dst)\n",
    "\n",
    "# read_size, write_size, reduce_size, offset\n",
    "blinkled.saxi.write(2 * 4, read_size)\n",
    "blinkled.saxi.write(3 * 4, write_size)\n",
    "blinkled.saxi.write(4 * 4, reduce_size)\n",
    "blinkled.saxi.write(5 * 4, bias_addr)\n",
    "\n",
    "# start\n",
    "blinkled.saxi.write(0 * 4, 1)\n",
    "\n",
    "# busy wait\n",
    "while True:\n",
    "    busy = blinkled.saxi.read(1 * 4)\n",
    "    if not busy:\n",
    "        break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6472845 6588493 6705165 6822861 6941581 7061325 7182093 7303885 7426701\n",
      " 7550541 7675405 7801293 7928205 8056141 8185101 8315085]\n"
     ]
    }
   ],
   "source": [
    "print(dst[-16:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6472845 6588493 6705165 6822861 6941581 7061325 7182093 7303885 7426701\n",
      " 7550541 7675405 7801293 7928205 8056141 8185101 8315085]\n"
     ]
    }
   ],
   "source": [
    "expected = np.sum(np.multiply(src, src).reshape([-1, reduce_size]), axis=-1) + bias\n",
    "print(expected[-16:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "diff_sum = np.sum(expected - dst)\n",
    "print(diff_sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
